/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.Result;
import com.jeeplus.config.properties.JeePlusProperites;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.guide.entity.MobileQueueType;
import com.jeeplus.modules.guide.entity.MobileUserYY;
import com.jeeplus.modules.guide.model.query.ModAppointment;
import com.jeeplus.modules.guide.model.vo.ModAppointmentOut;
import com.jeeplus.modules.guide.service.*;
import com.jeeplus.modules.guide.task.QueueOther;
import com.jeeplus.modules.guide.utils.HttpQueueUtils;
import com.jeeplus.modules.sys.entity.MobileUserQR;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.security.util.JWTUtil;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.EncryptUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 排队叫号对接Controller
 * @author azj
 * @version 2021-5-15
 */
@RestController
@RequestMapping("/mobilePublicSecurity")
@RequiredArgsConstructor
public class MobilePublicSecurityController extends BaseController {
	//对接AppKeyValue
	@Value("${AppKeyValue}")
	private String AppKeyValue;
	//排队叫号QueueUrl
	@Value("${QueueUrl}")
	private String QueueUrl;
	//私钥
	@Value("${GX_PUBLIC_KEY}")
	private String GX_PUBLIC_KEY;
	//公安KEY
	@Value("${gongAnKey}")
	private String gongAnKey;
	//公安户政点位ID
	@Value("${gongAnOrgrizeId}")
	private String gongAnOrgrizeId;
	//公安户政业务编号
	@Value("${gongAnTypeNumber}")
	private String gongAnTypeNumber;
    //公安户政业务编号
    @Value("${gongAnTypeNumberIdCardService}")
    private String gongAnTypeNumberIdCardService;
	//对接公安的排队叫号状态路径
	@Value("${gongAnUrl}")
	private String gongAnUrl;


	private final MobilePublicSecurityService mobilePublicSecurityService;
	private final MobilePublicSecurityTwoService mobilePublicSecurityTwoService;
	private final QueueOther queueOther;



	/**
	 * intoQueue 添加排队号（不自动生成排队编号）
	 *
	 * @version
	 */
	@GetMapping(value = "intoQueueOne")
	public AjaxJson intoQueueOne(@RequestHeader("AppKey") String AppKey,  String name, String idcard, String number,String type) {
//		type 1取号我们这边不呼叫 2预审通过我们这边可以叫号 3弃号
		if (AppKeyValue.equals(AppKey)) {
//			取号，暂时不叫号，把编号的是type设置为0
			if (type.equals("1")) {
			    if(number.indexOf("A")<=-1){
                    String result = HttpQueueUtils.intoQueueOne(QueueUrl, idcard, name, gongAnTypeNumber, gongAnOrgrizeId);
                    logger.error("公安取号数据 type:"+type+",number:" + number +",result: "+result);
                    return AjaxJson.success("操作成功！");
                }else{
                    String result = HttpQueueUtils.intoQueueOne(QueueUrl, idcard, name, gongAnTypeNumberIdCardService, gongAnOrgrizeId);
                    logger.error("公安身份证取号数据 type:"+type+",number:" + number +",result: "+result);
                    return AjaxJson.success("操作成功！");
                }

//				预审通过，我们这边可以叫号设置stype为1
			} else if (type.equals("2")) {
				logger.error("公安预审数据 type:"+type+",number:" + number );
				String result = HttpQueueUtils.passQueue(QueueUrl, idcard, gongAnOrgrizeId, number, "1");
				if(mobilePublicSecurityTwoService.setComeTime(number) >= 1){
                    logger.error("公安预审通过数据 type:"+type+",number:" + number +",result: "+result);
                }else{
					logger.error("公安预审失败数据 type:"+type+",number:" + number +",result: "+result);
				}
                return AjaxJson.success("操作成功！");
//				弃号
			} else if (type.equals("3")) {
//				HttpQueueUtils.passQueue(QueueUrl, idcard, gongAnOrgrizeId, number,"0");
				return AjaxJson.success("操作成功！");
			}

		} else {
			return AjaxJson.error("AppKey is error!").put("code", 400);
		}


		return null;
	}

	/**
	 *公安同步叫号，结束，弃号状态
	 *
	 * @return
	 */
	@GetMapping("queueTimeSynchronous")
	public AjaxJson queueTimeSynchronous(String type,String number){
		Map<String, Object> startTime = mobilePublicSecurityService.getStartTime(number);
		String date = DateUtils.formatDate(new Date(), DateUtils.PATTERN_YYYYMMDD);
		long time = new Date().getTime();
		startTime.put("date",date);
		startTime.put("time",time+"");
		startTime.put("type",type);
		String sign=type+date+startTime.get("number")+startTime.get("winnumber")+time+gongAnKey;
		sign = DigestUtils.md5DigestAsHex(sign.getBytes()).toUpperCase();
		startTime.put("sign",sign);
		String s = HttpQueueUtils.QueueSync(startTime,gongAnUrl);
		return AjaxJson.success(s);
	}

	//修改业务主题
	@PostMapping(value ="updateTypeTheme")
	public AjaxJson updateTypeTheme(String typeName,String typeId){
		if(mobilePublicSecurityTwoService.updateTypeTheme(typeName,typeId) >= 1){
			return AjaxJson.success();
		}else{
			return AjaxJson.error("操作失败");
		}
	}
	//修改业务部门
	@PostMapping(value ="updateTypeManager")
	public AjaxJson updateTypeManager(String managerName,String typeId){
		if(mobilePublicSecurityTwoService.updateTypeManager(managerName,typeId) >= 1){
			return AjaxJson.success();
		}else{
			return AjaxJson.error("操作失败");
		}
	}

	//同步业务主题至前置库
	@GetMapping(value ="updateTypeThemeToGX")
	public AjaxJson updateTypeThemeToGX(){
		queueOther.intoManager();
		return AjaxJson.success();
	}

}
